package mdt.program;

import org.tigris.gef.base.Layer;
import org.tigris.gef.graph.GraphEdgeRenderer;
import org.tigris.gef.graph.GraphModel;
import org.tigris.gef.graph.GraphNodeRenderer;
import org.tigris.gef.presentation.FigEdge;
import org.tigris.gef.presentation.FigNode;
import mdt.figure.*;
import mdt.model.*;

import java.util.Map;

public class MDTRenderer implements GraphNodeRenderer, GraphEdgeRenderer {

	public FigEdge getFigEdgeFor(GraphModel graphmodel, Layer layer, Object edge, Map attributeMap) {
		IEdgeModel model = null;
		FigEdge view = null;

		if (edge instanceof IMDTMessage) {
			model = (MDTMessage) edge;
			view = null;

			MDTMessage message = (MDTMessage)edge;
			if (edge instanceof ISendMessage) {
				view = new FigSendMessage(message, layer);
			} else if (edge instanceof IDualWayMessage) {
				view = new FigDualWayMessage(message, layer);
			} else if (edge instanceof IReplyMessage) {
				view = new FigReplyMessage(message, layer);
			} else if (edge instanceof IFailedMessage) {
				view = new FigFailedMessage(message, layer);
			}
		} else if (edge instanceof ICommentLink) {
			model = (ICommentLink) edge;
			view = new FigCommentLink((ICommentLink)model, layer);
		} else if (edge instanceof IArrowTextEdge) {
			model = (ITextEdge) edge;
			view = new FigArrowTextEdge((ITextEdge)model);
		} else if (edge instanceof ITextEdge) {
			model = (ITextEdge) edge;
			view = new FigTextEdge((ITextEdge)model);
		}

		if (model != null && view != null) {
			IConnector src = model.getSrcConnector();
			IConnector dst = model.getDstConnector();

			FigNode srcNode = getConnectorFigNode(layer, src);
			FigNode dstNode = getConnectorFigNode(layer, dst);

			view.setSourcePortFig(srcNode.getPortFig(src));
			view.setSourceFigNode(srcNode);
			view.setDestPortFig(dstNode.getPortFig(dst));
			view.setDestFigNode(dstNode);
			return view;
		}

		return null;
	}

	private FigNode getConnectorFigNode(Layer layer, IConnector connector) {
		FigNode node = (FigNode) layer.presentationFor(connector);
		if (node == null && connector != connector.getConnectorOwner()) {
			node = (FigNode) layer.presentationFor(connector.getConnectorOwner());
		}
		return node;
	}

	public FigEdge getFigEdgeFor(Object edge, Map attributeMap) {
		return null;
	}

	public FigNode getFigNodeFor(GraphModel graphmodel, Layer layer, Object node, Map attributeMap) {
		if (node instanceof INetworkDevice)
			return new FigNetworkDevice((INetworkDevice) node);
		else if (node instanceof IMDTComment)
			return new FigRichText((IMDTComment) node);

		return null;
	}

	public FigNode getFigNodeFor(Object node, int x, int y, Map attributeMap) {
		return null;
	}

	/*
	@Override
	public FigEdge getFigEdgeFor(GraphModel gm, Layer lay, Object edge) {
		return null;
	}

	@Override
	public FigNode getFigNodeFor(GraphModel gm, Layer lay, Object shapeNode) {
		if (shapeNode instanceof UmlClass) return new ClassNodeFig(shapeNode);
		
		return null;
	}
	*/

}
